package com.ysu.zyw.controller;

import com.ysu.zyw.other.exception.KnownException;
import com.ysu.zyw.other.security.BlogUserDetails;
import com.ysu.zyw.service.BlogMessageService;
import com.ysu.zyw.service.BlogPageService;
import com.ysu.zyw.service.BlogPromptService;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zhang_yao_wu1993 on 15/4/24.
 *
 * @author by Zerur
 */
@Controller
public class BlogPageController {
    private BlogPromptService blogPromptService;
    private BlogPageService blogPageService;

    public BlogPromptService getBlogPromptService() {
        return blogPromptService;
    }

    @Resource(name = "blogPromptService")
    public void setBlogPromptService(BlogPromptService blogPromptService) {
        this.blogPromptService = blogPromptService;
    }

    public BlogPageService getBlogPageService() {
        return blogPageService;
    }

    @Resource(name = "blogPageService")
    public void setBlogPageService(BlogPageService blogPageService) {
        this.blogPageService = blogPageService;
    }




    @RequestMapping("/LoginPage")
    public String toUserLoginPage() {
        // none
        return "/WEB-INF/view/blog_Login.jsp";
    }

    @RequestMapping("/RegisterPage")
    public String toUserRegisterPage() {
        // none
        return "/WEB-INF/view/blog_Register.jsp";
    }

    @RequestMapping("/MainPage")
    public String toMainPage() {
        return "/MainPage/0/1";
    }

    @RequestMapping("/MainPage/{category}/{page}")
    public ModelAndView toMainPageDelegate(@PathVariable String category, @PathVariable String page) {
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Main.jsp");
        blogPageService.setBlogMainPageModelAndView(modelAndView, category, page);
        return modelAndView;
    }

    @RequestMapping("/MessagePage")
    public String toMessagePage() {
        return "redirect:MessagePage/1";
    }

    @RequestMapping("/MessagePage/{page}")
    public ModelAndView toMessagePageDelegate(@PathVariable String page) {
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Message.jsp");
        blogPageService.setBlogMessagePageModelAndView(modelAndView, page);
        return modelAndView;
    }

    @RequestMapping("/MessageTitle")
    public String MessageTitle(HttpServletResponse response) {
        response.setHeader("X-Frame-Options", "SAMEORIGIN");
        return "/WEB-INF/view/wiz-html/MessageTitle.jsp";
    }

    @RequestMapping("/IndividualCenter")
    public ModelAndView toIndividualCenter() {
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_IndividualCenter.jsp");
        blogPageService.setBlogIndividualCenterModelAndView(modelAndView);
        return modelAndView;
    }

    @RequestMapping("/ManagePage")
    public ModelAndView toManagerPage() {
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Manage.jsp");
        blogPageService.setBlogManagePageModelAndView(modelAndView);
        return modelAndView;
    }

    @RequestMapping("/ArticlePage/{uuid}")
    public ModelAndView toArticlePage(@PathVariable String uuid) {
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Article.jsp");
        blogPageService.setBlogArticlePageModelAndView(modelAndView, uuid);
        return modelAndView;
    }

    @RequestMapping("/ArticlePageDelegate/{uuid}")
    public String toArticlePageDelegate(@PathVariable String uuid, HttpServletResponse response) {
        response.setHeader("X-Frame-Options", "SAMEORIGIN");
        return "/WEB-INF/view/wiz-html/" + uuid + ".jsp";
    }

    @RequestMapping("/UploadPage")
    public ModelAndView toUploadPage() {
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Upload.jsp");
        blogPageService.setBlogUploadPageModelAndView(modelAndView);
        return modelAndView;
    }


    @RequestMapping("/SuccessPromptPage")
    public ModelAndView toSuccessPromptPage() {
        ModelAndView modelAndView = new ModelAndView("WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillSuccessPromptModelAndView(modelAndView);
        return modelAndView;
    }

    @RequestMapping("/FailedPromptPage")
    public ModelAndView toFailedPromptPage() {
        ModelAndView modelAndView = new ModelAndView("WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillFailedPromptModelAndView(modelAndView);
        return modelAndView;
    }

    @RequestMapping("/InsufficientPermissions")
    public ModelAndView insufficientPermissions() {
        ModelAndView modelAndView = new ModelAndView("WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillInsufficientPermissionsModelAndView(modelAndView);
        return modelAndView;
    }

    @RequestMapping("/PageNotFound")
    public ModelAndView pageNotFound() {
        ModelAndView modelAndView = new ModelAndView("WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillPageNotFoundModelAndView(modelAndView);
        return modelAndView;
    }

    @RequestMapping("/UnknownException")
    public ModelAndView unknownException() {
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillUnknownExceptionPromptModelAndView(modelAndView);
        return modelAndView;
    }


    @ExceptionHandler(AccessDeniedException.class)
    public ModelAndView handlerAccessDeniedException(AccessDeniedException e) {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillInsufficientPermissionsModelAndView(modelAndView);
        return modelAndView;
    }

    @ExceptionHandler(KnownException.class)
    public ModelAndView handlerKnownException(KnownException e) {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillKnownExceptionPromptModelAndView(modelAndView, e);
        return modelAndView;
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView handlerUnknownException(Exception e) {
        e.printStackTrace();
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/view/blog_Prompt.jsp");
        blogPromptService.fillUnknownExceptionPromptModelAndView(modelAndView);
        return modelAndView;
    }
}
